package org.mapdb

import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import kotlin.random.Random

/**
 *
 * @author qinpeifa
 */

fun main(args: Array<String>){
    println("hello world")
    val executeService = Executors.newScheduledThreadPool(2);
    val db = DBMaker.memoryDirectDB().make()
    val hashMap = db.hashMap("hashmap").keySerializer(Serializer.STRING)
        .valueSerializer(Serializer.STRING).expireAfterCreate(10000, TimeUnit.SECONDS)
        .expireAfterUpdate(10000, TimeUnit.SECONDS)
        .counterEnable()
        .expireStoreSize(1 * 1024 * 1024)
        .expireExecutorPeriod(5000)
        .expireExecutor(executeService)
        .createOrOpen();


    for (i in 1..10){
        val random = Random(i);
        val before = System.currentTimeMillis();
        hashMap.put(random.nextDouble().toString(), random.nextDouble().toString())
        val after = System.currentTimeMillis();
        println("i: " + i + " time: " + (after - before));
        Thread.sleep(random.nextInt(1000) + 500L);
    }

    println("end")
    hashMap.close()


}